package com.lengjing.project.config;

import com.lengjing.project.component.LoginHandleInterceptor;
import com.lengjing.project.component.MyLocaleResover;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Description : mvc 扩展配置类,需要标注@Configuration 并继承 WebMvcConfigurerAdapter
 *                  WebMvcConfigurerAdapter : 可以来扩展Mvc功能
 * @Author : ChenYao
 * @Version : 1.0
 * @Create : Date Time: 2018/6/25 18:12
 * @Update : Date Time: 2018/6/25 18:12
 * @see :
 */
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //super.addViewControllers(registry);
        //浏览器发送xiaoye 请求,返回success.html页面,也是由模板引擎来解析的;
        registry.addViewController("/xiaoye").setViewName("success");
    }

    /**
     * @Description: 所有的WebMvcConfigurerAdapter组件都会一起起作用
     * @Author: ChenYao
     * @param:
     * @return: 
     * @throws: 
     * @CreateDate: 2018/6/26 14:38
     * @see: 
     * Note:
     */
    
    @Bean//将组件添加到容器中
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                //super.addViewControllers(registry);
                registry.addViewController("/").setViewName("login");
                registry.addViewController("login.html").setViewName("login");
                registry.addViewController("main.html").setViewName("dashboard");
            }
            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
               // super.addInterceptors(registry);
                //静态资源,*.css,*.js; Spring boot 已经做好静态资源的映射,可以直接访问;
                registry.addInterceptor(new LoginHandleInterceptor()).addPathPatterns("/**").excludePathPatterns("/login.html","/","/user/login");

            }
        };
        return webMvcConfigurerAdapter;
    }
    //把自己配置的国际化信息添加到容器中
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResover();
    }
}
